﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Petroleum.Tool;
using HZH_Controls.Controls;
using Petroleum.DB.Model;
using System.Text.RegularExpressions;
using Petroleum.Model;
using Sunny.UI;
using Newtonsoft.Json;
using System.Data.SQLite;
using HZH_Controls.Forms;

namespace Petroleum.UC
{
    public partial class Step12 : UserControl
    {
        FormMain main = new FormMain();
        List<Casing> casings = new List<Casing>();
        public Step12(FormMain main)
        {
            InitializeComponent();
            this.main = main;
            txtline.TextChanged += Txtline_TextChanged;
            txtline1.TextChanged += Txtline1_TextChanged;
            txtline2.TextChanged += Txtline2_TextChanged;
            InitData();
        }
        private void Txtline_TextChanged(object sender, EventArgs e)
        {
            try
            {
                lblTotal.Text = (double.Parse(txtline.InputText) * 1.488154 * double.Parse(lbllength.Text)).ToString();
                lbltotaltwo.Text = Math.Round(decimal.Parse(lblTotal.Text), 2).ToString();
            }
            catch (Exception ex)
            {

            }
        }
        private void Txtline1_TextChanged(object sender, EventArgs e)
        {
            try
            {
                lblTotal1.Text = (double.Parse(txtline1.InputText) * 1.488154 * double.Parse(lbllength1.Text)).ToString();
                lbltotaltwo1.Text = Math.Round(decimal.Parse(lblTotal1.Text), 2).ToString();
            }
            catch (Exception ex)
            {

            }
        }
        private void Txtline2_TextChanged(object sender, EventArgs e)
        {
            try
            {
                lblTotal2.Text = (double.Parse(txtline2.InputText) * 1.488154 * double.Parse(lbllength2.Text)).ToString();
                lbltotaltwo2.Text = Math.Round(decimal.Parse(lblTotal2.Text), 2).ToString();
            }
            catch (Exception ex)
            {

            }
        }
        void InitData()
        {
            casings = Session.CasingProgram.CasingList;
            for (int i = 0; i < casings.Count; i++)
            {
                if (i == 0)
                    InitControl(txtno, lblInterval, lblcasingSize, lbllength, casings[i], txtconn, txtgrade, txtline, txtthick, txtcolla, txtburs, txttensile, lblTotal, lbltotaltwo);
                else if (i == 1)
                {
                    panel2.Visible = true;
                    InitControl(txtno1, lblInterval1, lblcasingSize1, lbllength1, casings[i], txtconn1, txtgrade1, txtline1, txtthick1, txtcolla1, txtburs1, txttensile1, lblTotal1, lbltotaltwo1);
                }
                else
                {
                    panel3.Visible = true;
                    InitControl(txtno2, lblInterval2, lblcasingSize2, lbllength2, casings[i], txtconn2, txtgrade2, txtline2, txtthick2, txtcolla2, txtburs2, txttensile2, lblTotal2, lbltotaltwo2);
                }
            }
        }
        void InitControl(UCTextBoxEx lblSpudNo, Label lblInterval, Label lblcasingSize1, Label lbllength, Casing c, UCTextBoxEx conn, UCTextBoxEx grade, UCTextBoxEx line, UCTextBoxEx thick, UCTextBoxEx colla, UCTextBoxEx burs, UCTextBoxEx tensile, Label total, Label totaltwo)
        {
            if (string.IsNullOrEmpty(c.Casingpro))
                lblSpudNo.InputText = c.SpudNo;
            else
                lblSpudNo.InputText = c.Casingpro;
            lblInterval.Text = c.CasingInterval;
            lblcasingSize1.Text = c.CasingSize;
            lbllength.Text = (double.Parse(c.CasingIntervalTo) - double.Parse(c.CasingIntervalFrom)) + "";
            conn.InputText = c.Conn;
            grade.InputText = c.Grade;
            line.InputText = c.Line;
            thick.InputText = c.Thickness;
            colla.InputText = c.Collapsing;
            burs.InputText = c.Burs;
            tensile.InputText = c.Tensile;
            total.Text = c.Total;
            totaltwo.Text = c.TotalTwo;
        }
        void SaveData(Casing c)
        {
            if (c.CasingInterval == lblInterval.Text)
            {
                c.Casingpro = txtno.InputText;
                c.Conn = txtconn.InputText;
                c.Grade = txtgrade.InputText;
                c.Line = txtline.InputText;
                c.Thickness = txtthick.InputText;
                c.Collapsing = txtcolla.InputText;
                c.Burs = txtburs.InputText;
                c.Tensile = txttensile.InputText;
                c.Total = lblTotal.Text;
                c.TotalTwo = lbltotaltwo.Text;
                c.casinglength = lbllength.Text;
            }
            if (c.CasingInterval == lblInterval1.Text)
            {
                c.Casingpro = txtno1.InputText;
                c.Conn = txtconn1.InputText;
                c.Grade = txtgrade1.InputText;
                c.Line = txtline1.InputText;
                c.Thickness = txtthick1.InputText;
                c.Collapsing = txtcolla1.InputText;
                c.Burs = txtburs1.InputText;
                c.Tensile = txttensile1.InputText;
                c.Total = lblTotal1.Text;
                c.TotalTwo = lbltotaltwo1.Text;
                c.casinglength = lbllength1.Text;
            }
            if (c.CasingInterval == lblInterval2.Text)
            {
                c.Casingpro = txtno2.InputText;
                c.Conn = txtconn2.InputText;
                c.Grade = txtgrade2.InputText;
                c.Line = txtline2.InputText;
                c.Thickness = txtthick2.InputText;
                c.Collapsing = txtcolla2.InputText;
                c.Burs = txtburs2.InputText;
                c.Tensile = txttensile2.InputText;
                c.Total = lblTotal2.Text;
                c.TotalTwo = lbltotaltwo2.Text;
                c.casinglength = lbllength2.Text;
            }
        }
        private void btnDown_BtnClick(object sender, EventArgs e)
        {
            SaveData();
            main.AddControl(new Step13(main));
        }
        private void btnUp_BtnClick(object sender, EventArgs e)
        {
            main.AddControl(new Step11(main));
        }
        void SaveData()
        {
            if (casings != null && casings.Count > 0)
            {
                foreach (var item in casings)
                {
                    SaveData(item);
                }
                Session.CasingProgram.CasingList = casings;
            }
        }
        private void btnsave_BtnClick(object sender, EventArgs e)
        {
            SaveData();
            SQLiteDataReader reader = main.sqliteHelper.ReadFullTable("SessionData");
            var projects = EntityTranslate.Translate<SessionData>(reader);
            var FormationPressure = projects.Where(n => n.TableName == TableNames.CasingProgram.ToString()).FirstOrDefault();
            if (FormationPressure != null)
                SQLiteHelper.SaveData(main.sqliteHelper, TableNames.CasingProgram.ToString(), JsonConvert.SerializeObject(Session.CasingProgram));
            else
                SQLiteHelper.SaveData(main.sqliteHelper, TableNames.CasingProgram.ToString(), JsonConvert.SerializeObject(Session.CasingProgram), 1);
            FrmTips.ShowTipsSuccess(new FormMain(), "数据保存成功！");
        }
        private void btnword_BtnClick(object sender, EventArgs e)
        {
            var casing = BaseModel.GetTableData(main.projects, TableNames.CasingProgram.ToString());
            if (casing != null)
            {
                Session.CasingProgram = JsonConvert.DeserializeObject<CasingProgram>(casing);
                if (Session.CasingProgram != null && Session.CasingProgram.CasingList != null && Session.CasingProgram.CasingList.Count > 0)
                {
                    InitData();
                }
            }
        }
    }
}
